<?php

namespace app\common\business;

use app\common\lib\Str;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Filesystem;

class File
{

    public array $allowImageExt = ['jpg','jpeg','gif','png','bmp'];
    public array $allowVideoExt = ['avi','wmv','mpeg','mov','mp4','flv'];

    public function uploadFile($file): string
    {
        try{
            $size = 1024 * 1024 * 400;//400M
            $ext = implode(',',$this->allowImageExt).','.implode(',',$this->allowVideoExt);
            validate(['file'=>['fileSize'=>$size,'fileExt'=>$ext]])->check(['file'=>$file]);

            $filePath=Filesystem::disk('public')->putFile('uploads',$file);
        } catch (ValidateException $e){
            throw new Exception($e->getMessage());
        }

        $domain = Str::globalRequestDomain();
        return $domain.'/'.str_replace(DIRECTORY_SEPARATOR,'/',$filePath);
    }
}
